package wili.exp.simple;

import java.util.Map;

import wili.c.Ca;
import wili.c.Math9a;
import wili.exp.simple.antlr.opExpParser.NumContext;

/**
 * 
 * @author weilai
 *
 */
class Svalue extends Sexp {
	Number num;
	Boolean is;

	public String toString() {
		if (num != null)
			return num.toString();
		return is ? "Y" : "N";
	}

	public String toNumString(Map cont) {
		return this.toString();
	}

	public Svalue(Node node) {
		super(node);
		if (node.tree instanceof NumContext) {
		} else {
		}
		String st = node.getContent();
		if (st.equals("true") || st.equals("Y")) {
			is = true;
			return;
		}
		if (st.equals("false") || st.equals("N")) {
			is = false;
			return;
		}
		Ca.asert(st != null, "");
		num = Math9a.numOf(st);
		if (num == null) {
			is = Boolean.parseBoolean(st);
		}
	}

	public Svalue(Boolean is) {
		super("");
		this.is = is;
	}

	public Svalue(Number num) {
		super("");
		this.num = num;
	}

	@Override
	public Boolean isTrue(Map cont) {
		return is;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T value(Map cont) {
		if (is != null)
			return (T) is;
		return (T) num;
	}

	public void setValue(Object va) {
		if (va == null) {
			this.num = null;
			this.is = null;
			return;
		}
		if (va instanceof Number) {
			this.num = (Number) va;
		} else {
			this.is = (Boolean) va;
		}
	}
}